home *** CD-ROM | disk | FTP | other *** search
/ Celestin Apprentice 5 / Apprentice-Release5.iso / Source Code / Libraries / SAT 2.3.8 / Demos / SAT Invaders demo ƒ / main.c < prev    next >
Text File  |  1995-09-13  |  9KB  |  363 lines

  1. //• C translation from Pascal source file: main.p
  2.  
  3. //• ================================================.
  4. //• =============== SATInvaders main unit ================.
  5. //• ================================================.
  6.  
  7. //• Example file for Ingemars Sprite Animation Toolkit.
  8. //• © Ingemar Ragnemalm 1992.
  9. //• See doc files for legal terms for using this code.
  10.  
  11. //• SATInvaders is a very simple game demonstrating how to use the Sprite Animation.
  12. //• Toolkit. It is intended as a minimal demonstration, without many features and options.
  13. //• that the other sample program, HeartQuest, has. No high scores or even score, only.
  14. //• one life, doesn't save settings, only one kind of enemy, no special effects like explosions.
  15. //• etc.
  16.  
  17. //• main SATInvaders.c
  18.  
  19. #include <TransSkel.h>
  20. #include <SAT.h>
  21. #include "InvadeSAT.h"
  22. //#include "GameGlobals.h"
  23. //• SoundConst, sPlayer, sEnemy, sShot, sMissile;
  24.  
  25. // All the following is now in InvadeSAT.h
  26. //extern void        InitEnemy(void);
  27. //extern pascal void        SetupEnemy(SpritePtr sp);
  28. //extern pascal void        HandleEnemy(SpritePtr me);
  29. //extern void        InitPlayer(void);
  30. //extern pascal void        SetupPlayer(SpritePtr player);
  31. //extern pascal void        HandlePlayer(SpritePtr me);
  32.  
  33. Boolean soundFlag, plotFastFlag, starFieldFlag;
  34.  
  35. //• --------------------------------------------------------------------.
  36. //•                     Game driver procedures                                .
  37. //• --------------------------------------------------------------------.
  38.  
  39. //• Setup a new level. This is called when the game starts and at each new level.
  40. void SetUpLevel (short level)
  41. {
  42.     short i, j;
  43.     SpritePtr sp;
  44.     
  45.     //• Clear the Sprite list.
  46.     while (gSAT.sRoot) SATKillSprite(gSAT.sRoot);
  47.     
  48.     missileCount = 0;     //• global count variable
  49.  
  50.     //• Create all the enemy sprites for the level, depending on the level number.
  51.     for (i = 0; i <= (level + 1); i++)
  52.         for (j = 0; j <= (level / 2) + 1; j++)
  53.             sp = SATNewSprite (-3, i * 40 + 2, j * 40 - 40 * (level / 2 + 1), &SetupEnemy);
  54.  
  55.     //• Make the player sprite.
  56.     sp = SATNewSprite (2, gSAT.offSizeH / 2, gSAT.offSizeV - 40, &SetupPlayer);
  57.  
  58.     //• Copy backScreen to offScreen to erase old sprites.
  59.     CopyBits (&(gSAT.backScreen.port->portBits), &(gSAT.offScreen.port->portBits), &(gSAT.offScreen.port->portRect), &(gSAT.offScreen.port->portRect), srcCopy, 0L);
  60.     SATRedraw ();
  61. }     //• SetUp Level.
  62.  
  63. //• Start a new game. Initialize level, score, number of lives, and call SetUp Level to make the first level.
  64. void StartGame ()
  65. {
  66.     level = 1;
  67.     SetUpLevel (level);
  68. }
  69.  
  70. void DoFileMenu (short item)
  71. {
  72.     switch (item)
  73.     {
  74.         case run: 
  75.         {
  76.         //• Test if we have Color QD, and if so, test bit depth! Alert if ((**features).PlotFast)
  77.             if (!((gSAT.initDepth == 1) || 
  78.                   (gSAT.initDepth == 4) || 
  79.                   (gSAT.initDepth == 8)) && plotFastFlag)
  80.             {
  81.                 SATReportStr ("\pPlease uncheck 'Fast animation' or set the monitor to b/w, 4-bit or 8-bit mode in the Control Panel.");
  82.                 return;
  83.             }
  84.             if (SATDepthChangeTest())     //• Update if necessary.
  85.                 ;
  86.             StartGame ();
  87.             ShowWindow (gSAT.wind.port);
  88.             SelectWindow (gSAT.wind.port);
  89.             GameWindUpdate (false, 0);
  90.             MoveIt ();
  91.         }
  92.         break;
  93.         
  94.         case sound: 
  95.         {
  96.             soundFlag = ! soundFlag;
  97.             CheckItem (fileMenu, sound, soundFlag);
  98.             if (soundFlag)     //• Tell the sound package our settings, so we don't have to bother.
  99.                 SATSoundOn();
  100.             else
  101.                 SATSoundOff();
  102.         }
  103.         break;
  104.  
  105.         case fastAnimation: 
  106.         {
  107.             plotFastFlag = ! plotFastFlag;
  108.             CheckItem (fileMenu, fastAnimation, plotFastFlag);
  109.         }
  110.         break;
  111.         
  112.         case starField: 
  113.         {
  114.             starFieldFlag = ! starFieldFlag;
  115.             CheckItem (fileMenu, starField, starFieldFlag);
  116.             ToggleStarField(starFieldFlag);
  117.         }
  118.         break;
  119.         
  120.         case quit: 
  121.             SkelWhoa ();
  122.         break;
  123.     }
  124. }
  125.  
  126. void MoveIt (void)
  127. {
  128.     long t;
  129.     EventRecord theEvent;     //• för att testa musklick.
  130.  
  131.     stillRunning = true;     
  132.  
  133.     //• Hide cursor and menu bar
  134.     //• NOTE: No matter how we leave the MoveIt procedure, we should 
  135.     //• ShowCursor and ShowMBar!
  136.     HideCursor ();
  137.     SATHideMBar(gSAT.wind.port);
  138.     SATRedraw();    //• We must redraw the menu bar area. I'm lazy and redraw it all.
  139.  
  140.     //• Main loop! Keep running until the game is paused or ends.
  141.     while (stillRunning == true)
  142.     {
  143.         t = TickCount ();
  144.  
  145.         if (starFieldFlag)
  146.             DoStars();
  147.  
  148.         //• Here is the real heart of the loop: call Animator once per loop. 
  149.         //• It will call all the objects.
  150.         SATRun (plotFastFlag);
  151.  
  152.         //• All the rest of the main loop is game specific, next level, 
  153.         //• bonus handling, etc.
  154.         if (globalSpeed.h == 0)
  155.         {
  156.             downCount--;
  157.             if (downCount <= 0)
  158.             {
  159.                 globalSpeed.h = - last_H;
  160.                 globalSpeed.v = 0;
  161.                 turnFlag = false;
  162.             };
  163.         }
  164.         else 
  165.             if (turnFlag)
  166.             {
  167.                 downCount = 10;
  168.                 last_H = globalSpeed.h;
  169.                 globalSpeed.h = 0;
  170.                 globalSpeed.v = 3;
  171.             };
  172.         if (! gSAT.anyMonsters)
  173.         {
  174.             SATSoundShutup  ();
  175.             level++;
  176.             SetUpLevel (level);
  177.         }     //• if not anyMonsters.
  178.  
  179.         //• Check for keys being pressed - but don't allow background 
  180.         //• processing.
  181.         //• If you want background processing, either use 
  182.         //• GetNextEvent+SystemTask or WaitNextEvent (the modern call).
  183.         if (GetOSEvent (keyDownMask, &theEvent))     //• keydown.
  184.             if ((theEvent.modifiers & cmdKey) != 0)
  185.                 switch ((theEvent.message & charCodeMask))
  186.                 {
  187.                     case 'q': 
  188.                     {
  189.                         SkelWhoa ();
  190.                         //• Do all the things we have to do when we 
  191.                         //• leave MoveIt!.
  192.                         SATSoundShutup ();     //• Dispose of sound channel.
  193.                         FlushEvents (everyEvent, 0);     //• To forget events, like mouse clicks etc.
  194.                         ShowCursor ();
  195.                         SATShowMBar(nil);
  196.                         return;
  197.                     }
  198.                     break;
  199.                     case 's': 
  200.                     {
  201.                         DoFileMenu (sound);
  202.                     }
  203.                     break;
  204.                     
  205.                     default:
  206.                     break;
  207.                 };     //• switch.
  208.         
  209.         //• Delay, using TickCount so it doesn't matter how fast 
  210.         //• our Mac is.
  211.         while ((TickCount () - t) < 3);
  212.  
  213.     } //while stillRunning; (main loop).
  214.  
  215.     while (! SATSoundDone() )
  216.         SATSoundEvents ();     //• Wait for last sound to complete.
  217.  
  218.     ShowCursor ();
  219.     SATShowMBar(nil);
  220.     FlushEvents (everyEvent, 0);     //• To forget Events, like mouse clicks etc.
  221.  
  222.     SATReportStr ("\pSorry, game over.");
  223.  
  224.     SATSoundShutup ();     //• Dispose of sound channel.
  225. }     //• MoveIt.
  226.  
  227. void GameWindUpdate (Boolean resized, short mods)
  228. {
  229.     CursHandle watch;
  230.  
  231.     watch = GetCursor (watchCursor); /* WatchCursor */
  232.     SetCursor (*watch);
  233.     if (SATDepthChangeTest() )
  234.     {
  235.         ;
  236.     }
  237.     InitCursor ();
  238.     ReleaseResource ((Handle) watch);
  239.  
  240.     SATRedraw ();
  241. } //GameWindUpdate
  242.  
  243. //• Process selection from File menu.
  244.  
  245. void GameWindIdle ()
  246. {
  247. }
  248.  
  249. void GameWindInit (void)
  250. {
  251.     Boolean dummy;
  252.     
  253.     //• Tell TransSkel to tell us when to update gSAT.wind.
  254.     dummy = SkelWindow (gSAT.wind.port, 0L, 0L, &GameWindUpdate, 0L, 0L, 0L, &GameWindIdle, false);
  255.  
  256.     //• Set up the two offScreen GrafPorts "offScreen" and "backScreen". SAT has a standard.
  257.     //• way to do this. Let SAT draw the background PICT for us, too.
  258.  
  259.     //• Call the Init routines for all the sprite units!.
  260.     InitEnemy ();
  261.     InitPlayer ();
  262.     InitMissile ();
  263.     InitShot ();
  264.  
  265.     // We must show the window ourselves when using SATCustomInit
  266.     ShowWindow (gSAT.wind.port);
  267.     SelectWindow (gSAT.wind.port);
  268.     //• Draw the contents of the window (to give the user something to 
  269.     //• look at during the rest of startup).
  270.     SATRedraw ();
  271. }
  272.  
  273. //• --------------------------------------------------------------------.
  274. //•             Menu handling procedures                        .
  275. //• --------------------------------------------------------------------.
  276.  
  277. //• Handle selection of "About…" item from Apple menu.
  278.  
  279. void DoAbout (void)
  280. {
  281.     short ignore;
  282.     Str255 versionString;
  283.  
  284.     SATGetVersion(versionString);
  285.     ParamText(versionString, "\p", "\p", "\p");
  286.     ignore = Alert (aboutAlrt, 0L);
  287. }
  288.  
  289. //• Initialize menus.  Tell TransSkel to process the Apple menu.
  290. //• automatically, and associate the proper procedures with the.
  291. //• File menu.
  292.  
  293. void SetUpMenus ()
  294. {
  295.     SkelApple ("\pAbout SAT Invaders…", &DoAbout);
  296.     fileMenu = GetMenu (fileMenuRes);
  297.     SkelMenu (fileMenu, &DoFileMenu, 0L, false, true);
  298.     //• Set the following flags so they match the menu.
  299.     soundFlag = true;
  300.     plotFastFlag = true;
  301. }
  302.  
  303. //• Hide gamewindow on suspend, so the user can get access to disk icons etc.
  304.  
  305. void DoSuspendResume (Boolean b)
  306. {
  307.     if (b)
  308.     {
  309.         ShowWindow (gSAT.wind.port);
  310.         SelectWindow (gSAT.wind.port);
  311.     }
  312.     else
  313.         HideWindow (gSAT.wind.port);
  314. }
  315.  
  316. Boolean DoEvt (EventRecord *e)
  317. {
  318.     if (e->what == osEvt)
  319.     {
  320.         if (((e->message, 8), 0xFF) == suspendResumeMessage)
  321.             DoSuspendResume ((e->message, 1) != 0);
  322.         return true;
  323.     }
  324.     else
  325.         return false;
  326. }     //• end DoEvent *.
  327.  
  328. //• --------------------------------------------------------------------.
  329. //•                             Main                                .
  330. //• --------------------------------------------------------------------.
  331. void main (void)
  332. {
  333.     Rect    gameArea;
  334.     
  335.     SkelInit (nil, 6);                //• Initialize.
  336.  
  337.     //• Init all the different parts of the game.
  338.     SetUpMenus ();        //• install menu handlers.
  339.     
  340.     SetRect(&gameArea, 0, 0, 512, 342);
  341.  
  342.     //• We use SATCustomInit to cover the full screen INCLUDING menu bar area!
  343.     SATCustomInit(129, 128, &gameArea, nil, nil, true, true, true, true, true);
  344. //SATInit (129, 128, 512, 322);
  345.  
  346.     GameWindInit ();    //• Init the game window.
  347.     LoadSounds ();        //• preload all sound resources.
  348.  
  349.     //• Set the randseed to something that is random enough.
  350. #ifdef THINKC
  351.     randSeed = TickCount ();
  352. #else
  353.     qd.randSeed = TickCount ();
  354. #endif
  355.  
  356.     SkelEventHook (&DoEvt); //• handle MultiFinder-Events.
  357.  
  358.     SkelMain ();                //• loop 'til Quit selected.
  359.     SkelClobber ();                //• clean up.
  360.     SATSoundShutup ();            //• Terminate sounds.
  361. }
  362.  
  363.